package com.yq.cp.controller.basics;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.MyException;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.EquipmentCategory;
import com.yq.cp.entity.basics.EquipmentManagement;
import com.yq.cp.entity.basics.EquipmentManagementExtend;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.User;
import com.yq.cp.mapper.basics.EquipmentManagementMapper;
import com.yq.cp.mapper.data.NengKongDataDayMapper;
import com.yq.cp.service.basics.EquipmentCategoryService;
import com.yq.cp.service.basics.EquipmentManagementExtendService;
import com.yq.cp.service.basics.EquipmentManagementService;
import com.yq.cp.service.basics.OrganizationalStructureService;
import com.yq.cp.utils.BeanCopyUtil;
import com.yq.cp.utils.DateTimeUtil;
import com.yq.cp.utils.RandomNumber;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 设备管理 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/equipmentManagement")
public class EquipmentManagementController {
    @Resource
    EquipmentManagementService equipmentManagementService;
    @Resource
    EquipmentManagementMapper equipmentManagementMapper;
    @Resource
    EquipmentManagementExtendService equipmentManagementExtendService;
    @Resource
    OrganizationalStructureService organizationalStructureService;
    @Resource
    EquipmentCategoryService equipmentCategoryService;
    @Resource
    NengKongDataDayMapper nengKongDataDayMapper;
    /**
     * 添加编辑
     * @param equipmentManagement
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody EquipmentManagement equipmentManagement){
        if(Objects.isNull(equipmentManagement.getId()))
            equipmentManagement.setExamine(0);
        QueryWrapper<EquipmentManagement> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("sn",equipmentManagement.getSn());
        queryWrapper.eq("examine",1);
        if (equipmentManagement.getId()!=null){
            queryWrapper.notIn("id",equipmentManagement.getId());
        }
        boolean exists = equipmentManagementMapper.exists(queryWrapper);
        if (exists){
            throw new MyException("sn重复");
        }

        equipmentManagementService.saveOrUpdate(equipmentManagement);
        return ApiResult.success();
    }

    /**
     * 审核列表-通过未通过列表
     * @param vo
     * @return
     */
    @PostMapping("examineList")
    public ApiResult examineList(@RequestBody PageVo vo){
        IPage<EquipmentManagement> page = equipmentManagementService.examineList(vo);
        return ApiResult.success(page);
    }

    /**
     * 审核
     * @param equipmentManagement
     * @return
     */
    @PostMapping("examine1")
    public ApiResult examine(@RequestBody EquipmentManagement equipmentManagement){
        User user = UserThreadLocal.get();
        equipmentManagement.setExaminePerson(user.getNickname());
        equipmentManagement.setExamineDate(DateTimeUtil.dqsj());
        equipmentManagement.setExaminePersonSignature(user.getElectronicSignature());
        equipmentManagementService.saveOrUpdate(equipmentManagement);
        return ApiResult.success();
    }

    /**
     * 删除改状态
     * @param vo
     * @return
     */
    @PostMapping("removegai1")
    public ApiResult removegai(@RequestBody EquipmentManagement vo){
        vo.setDeleteExamine(0);
        equipmentManagementService.saveOrUpdate(vo);
        return ApiResult.success();
    }

    /**
     * 删除审核列表
     * @param vo
     * @return
     */
    @PostMapping("removeExamineList")
    public ApiResult removeExamineList(@RequestBody PageVo vo){
        IPage<EquipmentManagement> page = equipmentManagementService.removeExamineList(vo);
        return ApiResult.success(page);
    }

    /**
     * 删除
     * @param vo
     * @return
     */
    @PostMapping("remove1")
    public ApiResult remove(@RequestBody EquipmentManagement vo){
        if (vo.getDeleteExamine()==1)
            equipmentManagementService.removeById(vo);
        else {
            EquipmentManagement byId = equipmentManagementService.getById(vo);
            //byId.setDeleteExamine(null);
            equipmentManagementService.bohui(byId.getId());
            //equipmentManagementService.saveOrUpdate(byId);
        }
        return ApiResult.success();
    }



    /**
     * 添加改
     * @param equipmentManagement
     * @return
     */
    @PostMapping("addgai")
    public ApiResult addgai(@RequestBody EquipmentManagement equipmentManagement){
        equipmentManagement.setExamine(0);
        EquipmentManagementExtend extend = BeanCopyUtil.copyObject(equipmentManagement, EquipmentManagementExtend.class);
        equipmentManagementService.saveOrUpdate(equipmentManagement);
        extend.setGlsbId(equipmentManagement.getId());
        extend.setGllx(0);
        equipmentManagementExtendService.saveOrUpdate(extend);
        return ApiResult.success();
    }

    /**
     * 编辑改
     * @param equipmentManagement
     * @return
     */
    @PostMapping("update")
    public ApiResult update(@RequestBody EquipmentManagement equipmentManagement){
        equipmentManagement.setExamine(0);
        EquipmentManagement byId = equipmentManagementService.getById(equipmentManagement);
        EquipmentManagementExtend extend = BeanCopyUtil.copyObject(byId, EquipmentManagementExtend.class);
        equipmentManagementService.saveOrUpdate(equipmentManagement);
        extend.setGlsbId(equipmentManagement.getId());
        extend.setGllx(1);
        extend.setExamine(0);
        extend.setId(null);
        equipmentManagementExtendService.saveOrUpdate(extend);
        return ApiResult.success();
    }

    /**
     * 更换设备
     * @param equipmentManagement
     * @return
     */
    @PostMapping("genghuan")
    public ApiResult genghuan(@RequestBody EquipmentManagement equipmentManagement){
        equipmentManagement.setExamine(0);
        EquipmentManagement byId = equipmentManagementService.getById(equipmentManagement);
        EquipmentManagementExtend extend = BeanCopyUtil.copyObject(byId, EquipmentManagementExtend.class);
        equipmentManagementService.saveOrUpdate(equipmentManagement);
        extend.setGlsbId(equipmentManagement.getId());
        extend.setGllx(2);
        extend.setExamine(0);
        extend.setId(null);
        equipmentManagementExtendService.saveOrUpdate(extend);
        return ApiResult.success();
    }

    /**
     * 审核改
     * @param equipmentManagement
     * @return
     */
    @PostMapping("examine")
    public ApiResult examinegai(@RequestBody EquipmentManagement equipmentManagement){
        List<EquipmentManagementExtend> list = equipmentManagementExtendService.list(new QueryWrapper<EquipmentManagementExtend>()
                .eq("glsb_id", equipmentManagement.getId()).orderByDesc("create_time"));
        long random = RandomNumber.getRandom();
        User user = UserThreadLocal.get();
        if (equipmentManagement.getExamine()==1){
            equipmentManagement.setExaminePerson(user.getNickname());
            equipmentManagement.setExamineDate(DateTimeUtil.dqsj());
            equipmentManagement.setExaminePersonSignature(user.getElectronicSignature());
            equipmentManagementService.saveOrUpdate(equipmentManagement);
            EquipmentManagement byId = equipmentManagementService.getById(equipmentManagement);
            if (list.get(0).getGllx()==0){
                list.get(0).setExaminePerson(user.getNickname());
                list.get(0).setExamineDate(DateTimeUtil.dqsj());
                list.get(0).setExaminePersonSignature(user.getElectronicSignature());
                list.get(0).setExamine(1);
                list.get(0).setRefuseReason(equipmentManagement.getRefuseReason());
                equipmentManagementExtendService.saveOrUpdate(list.get(0));
                return ApiResult.success();
            }

            EquipmentManagementExtend extend = BeanCopyUtil.copyObject(byId, EquipmentManagementExtend.class);
            extend.setId(null);
            extend.setGlsbId(list.get(0).getGlsbId());
            extend.setGllx(list.get(0).getGllx());
            extend.setRandomId(random);
            equipmentManagementExtendService.saveOrUpdate(extend);
            list.get(0).setRandomId(random);
            equipmentManagementExtendService.saveOrUpdate(list.get(0));
            if (list.get(0).getGllx()==2){
                List<EquipmentManagementExtend> list1 = equipmentManagementExtendService.list(new QueryWrapper<EquipmentManagementExtend>()
                        .eq("glsb_id", equipmentManagement.getId()));
                EquipmentManagement equipment = BeanCopyUtil.copyObject(byId, EquipmentManagement.class);
                equipment.setId(null);
                equipmentManagementService.save(equipment);
                for (EquipmentManagementExtend managementExtend : list1) {
                    managementExtend.setGlsbId(equipment.getId());
                    equipmentManagementExtendService.saveOrUpdate(managementExtend);
                }
                equipmentManagementService.removeById(byId);
            }
        }else {
            if (list.get(0).getGllx()==0){
                equipmentManagement.setExaminePerson(user.getNickname());
                equipmentManagement.setExamineDate(DateTimeUtil.dqsj());
                equipmentManagement.setExaminePersonSignature(user.getElectronicSignature());
                equipmentManagementService.saveOrUpdate(equipmentManagement);
                equipmentManagementExtendService.removeById(list.get(0));
                return ApiResult.success();
            }
            if (list.get(0).getGllx()==1){
                equipmentManagement.setExamine(1);
                equipmentManagement.setName(list.get(0).getName());
                equipmentManagement.setEquipmentCode(list.get(0).getEquipmentCode());
                equipmentManagementService.saveOrUpdate(equipmentManagement);
                EquipmentManagementExtend extend = BeanCopyUtil.copyObject(list.get(0), EquipmentManagementExtend.class);
                extend.setExamine(2);
                extend.setExaminePerson(user.getNickname());
                extend.setExamineDate(DateTimeUtil.dqsj());
                extend.setExaminePersonSignature(user.getElectronicSignature());
                extend.setRefuseReason(equipmentManagement.getRefuseReason());
                extend.setRandomId(random);
                extend.setId(null);
                equipmentManagementExtendService.saveOrUpdate(extend);
                list.get(0).setRandomId(random);
                equipmentManagementExtendService.saveOrUpdate(list.get(0));
            }
            if (list.get(0).getGllx()==2){
                EquipmentManagement management = BeanCopyUtil.copyObject(list.get(0), EquipmentManagement.class);
                management.setId(equipmentManagement.getId());
                management.setExamine(1);
                equipmentManagementService.saveOrUpdate(management);

                EquipmentManagementExtend extend = BeanCopyUtil.copyObject(list.get(0), EquipmentManagementExtend.class);
                extend.setExamine(2);
                extend.setExaminePerson(user.getNickname());
                extend.setExamineDate(DateTimeUtil.dqsj());
                extend.setExaminePersonSignature(user.getElectronicSignature());
                extend.setRefuseReason(equipmentManagement.getRefuseReason());
                extend.setRandomId(random);
                extend.setId(null);
                equipmentManagementExtendService.saveOrUpdate(extend);
                list.get(0).setRandomId(random);
                equipmentManagementExtendService.saveOrUpdate(list.get(0));
            }

        }
        return ApiResult.success();
    }

    /**
     * 历史记录
     * @param equipmentManagement
     * @return
     */
    @PostMapping("lsjl")
    public ApiResult lsjl(@RequestBody EquipmentManagement equipmentManagement){
        List<EquipmentManagementExtend> list = equipmentManagementExtendService.list(new QueryWrapper<EquipmentManagementExtend>()
                .eq("glsb_id", equipmentManagement.getId())
                .orderByAsc("create_time"));
        List<EquipmentManagementExtend> list1 = new ArrayList<>();
        list1.add(list.get(0));
        if (list.size()>2){
            int i;
            if (list.size()%2==1)
                i = list.size()/2;
            else
                i = (list.size()-1)/2;
            for (int j = 0;j<i;j++){
                list.get(2*j+1).setEquipmentManagementExtend(list.get(2*j+2));
                list1.add(list.get(2*j+1));
            }
        }
        for (EquipmentManagementExtend equipmentManagementExtend : list1) {
            OrganizationalStructure byId = organizationalStructureService.getById(equipmentManagementExtend.getSiteId());
            equipmentManagementExtend.setSiteName(byId.getName());
            EquipmentCategory byId1 = equipmentCategoryService.getById(equipmentManagementExtend.getEquipmentCategoryId());
            equipmentManagementExtend.setEquipmentCategoryName(byId1.getName());
            if (!Objects.isNull(equipmentManagementExtend.getEquipmentType())){
                EquipmentCategory byId2 = equipmentCategoryService.getById(equipmentManagementExtend.getEquipmentType());
                equipmentManagementExtend.setEquipmentTypeName(byId2.getName());
                if (!Objects.isNull(equipmentManagementExtend.getEquipmentManagementExtend()))
                    equipmentManagementExtend.getEquipmentManagementExtend().setEquipmentTypeName(byId2.getName());
            }
            if (!Objects.isNull(equipmentManagementExtend.getEquipmentManagementExtend())){
                equipmentManagementExtend.getEquipmentManagementExtend().setSiteName(byId.getName());
                equipmentManagementExtend.getEquipmentManagementExtend().setEquipmentCategoryName(byId1.getName());
            }

            Long glsbId = equipmentManagementExtend.getGlsbId();
            BigDecimal cxljsj = nengKongDataDayMapper.cxljsj(glsbId, 119);
            equipmentManagementExtend.setFdl(cxljsj==null?new BigDecimal(0):cxljsj);

        }
        return ApiResult.success(list1);
    }

    /**
     * 删除改留历史记录
     * @param vo
     * @return
     */
    @PostMapping("removegai")
    public ApiResult removegaiTogai(@RequestBody EquipmentManagement vo){
        vo.setDeleteExamine(0);
        equipmentManagementService.saveOrUpdate(vo);
        EquipmentManagement byId = equipmentManagementService.getById(vo);
        EquipmentManagementExtend extend = BeanCopyUtil.copyObject(byId, EquipmentManagementExtend.class);
        extend.setGllx(3);
        extend.setGlsbId(vo.getId());
        extend.setExamine(0);
        extend.setId(null);
        equipmentManagementExtendService.saveOrUpdate(extend);
        return ApiResult.success();
    }

    /**
     * 删除-最后的操作
     * @param vo
     * @return
     */
    @PostMapping("remove")
    public ApiResult removezuihou(@RequestBody EquipmentManagement vo){
        if (vo.getDeleteExamine()==1){
            List<EquipmentManagementExtend> list = equipmentManagementExtendService.list(new QueryWrapper<EquipmentManagementExtend>()
                    .eq("glsb_id", vo.getId()).orderByDesc("create_time"));
            EquipmentManagementExtend extend = BeanCopyUtil.copyObject(list.get(0), EquipmentManagementExtend.class);
            extend.setExamine(1);
            extend.setExaminePerson(UserThreadLocal.get().getNickname());
            extend.setExamineDate(DateTimeUtil.dqsj());
            extend.setId(null);
            extend.setRefuseReason(vo.getRefuseReason());
            extend.setExaminePersonSignature(UserThreadLocal.get().getElectronicSignature());
            equipmentManagementExtendService.saveOrUpdate(extend);
            equipmentManagementExtendService.saveOrUpdate( list.get(0));
            equipmentManagementService.saveOrUpdate(vo);
            equipmentManagementService.removeById(vo);
        }
        else {
            long random = RandomNumber.getRandom();
            EquipmentManagement byId = equipmentManagementService.getById(vo);
            //byId.setDeleteExamine(null);
            equipmentManagementService.bohui(byId.getId());
            List<EquipmentManagementExtend> list = equipmentManagementExtendService.list(new QueryWrapper<EquipmentManagementExtend>()
                    .eq("glsb_id", vo.getId()).orderByDesc("create_time"));
            EquipmentManagementExtend extend = BeanCopyUtil.copyObject(list.get(0), EquipmentManagementExtend.class);
            extend.setExamine(2);
            extend.setExaminePerson(UserThreadLocal.get().getNickname());
            extend.setExamineDate(DateTimeUtil.dqsj());
            extend.setRandomId(random);
            extend.setRefuseReason(byId.getRefuseReason());
            extend.setExaminePersonSignature(UserThreadLocal.get().getElectronicSignature());
            extend.setId(null);
            equipmentManagementExtendService.saveOrUpdate(extend);
            list.get(0).setRandomId(random);
            equipmentManagementExtendService.saveOrUpdate( list.get(0));
        }
        return ApiResult.success();
    }

    /**
     * 已经删除的列表
     * @param vo
     * @return
     */
    @PostMapping("deleteToRemove")
    public ApiResult deleteToRemove(@RequestBody PageVo vo){
        IPage<EquipmentManagement> page = equipmentManagementService.deleteToRemove(vo);
        return ApiResult.success(page);
    }

    /**
     * 修改图片
     * @param
     */
    @PostMapping("editPhoto")
    public void editPhoto(@RequestBody EquipmentManagement equipmentManagement){

        equipmentManagementService.saveOrUpdate(equipmentManagement);
    }
}

